package com.steven.team.controller;

import com.alibaba.fastjson.JSONObject;
import com.steven.team.pojo.MatchInfo;
import com.steven.team.service.MatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class MatchController {

    @Autowired
    MatchService matchService;

    @PostMapping("/match/query")
    public JSONObject queryMatch(@RequestHeader Long uid, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return matchService.calculateMatch(uid);
    }

    @PostMapping("/match/detail")
    public JSONObject searchMatchDetailById(@RequestHeader Long uid, Long id, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return matchService.searchMatchDetailById(uid, id);
    }

    @PostMapping("/match/addHistory")
    public JSONObject addHistory(@RequestHeader Long uid, Long matchId, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return matchService.addHistory(uid, matchId);
    }

    @PostMapping("/match/searchTeam")
    public JSONObject searchTeamByMatch(@RequestHeader Long uid, Long matchId, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return matchService.searchTeamByMatch(matchId);
    }

    @PostMapping("/match/searchHistory")
    public JSONObject searchHistory(@RequestHeader Long uid, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return matchService.searchHistory(uid);
    }

    @PostMapping("/match/searchHot")
    public JSONObject searchTop10MatchRecent(@RequestHeader Long uid, HttpServletResponse response) throws IOException {
        return matchService.searchTop10MatchRecent();
    }

    @PostMapping("/match/collection/add")
    public JSONObject addToCollection(@RequestHeader Long uid, Long matchId, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return matchService.addToCollection(matchId, uid);
    }

    @PostMapping("/match/collection/delete")
    public JSONObject deleteCollectionById(@RequestHeader Long uid, Long matchId, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return matchService.deleteCollectionById(uid, matchId);
    }

    @PostMapping("/match/collection/query")
    public JSONObject selectCollectionsByUser(@RequestHeader Long uid, Long lastCreateTime, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return matchService.selectCollectionsByUser(uid, lastCreateTime);
    }

    @PostMapping("/match/add")
    public JSONObject addMatch(@RequestHeader Long uid, @RequestBody MatchInfo matchInfo, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return matchService.addMatch(uid, matchInfo);
    }
}
